package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.BookingRule;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        //参数转型
        String jsonString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        //根据hoscode查询医院信息
        Hospital targetHospital = hospitalRepository.getByHoscode(hospital.getHoscode());
        //查询到，修改
        if (targetHospital!=null){
            hospital.setId(targetHospital.getId());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(targetHospital.getIsDeleted());
            hospital.setStatus(targetHospital.getStatus());
            hospitalRepository.save(hospital);
        }else {
            //查询不到，新增
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setStatus(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getByHoscode(hoscode);
        if (hospital==null){
            throw new YyghException(20001, "医院信息有误");
        }
        return hospital;
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page-1, limit, sort);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        Page<Hospital> pageModel = hospitalRepository.findAll(example, pageable);
        pageModel.getContent().forEach(item->{
            this.packHospital(item);
        });
        return pageModel;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue()==0||status.intValue()==1){
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Map<String, Object> show(String id) {

        Hospital hospital = this.packHospital(hospitalRepository.findById(id).get()) ;
        //2从医院信息取出预约规则，清空重复数据
        BookingRule bookingRule = hospital.getBookingRule();
        hospital.setBookingRule(null);
        //3封装数据返回
        Map<String, Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",bookingRule);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = this.getByHoscode(hoscode);

        return hospital.getHosname();
    }

    @Override
    public List<Hospital> getByHosnameLike(String hosname) {
        List<Hospital> list = hospitalRepository.findByHosnameLike(hosname);
        return list;
    }

    @Override
    public Map<String, Object> getHospInfo(String hoscode) {

        Hospital hospital = this.packHospital(hospitalRepository.getByHoscode(hoscode));
        //2从医院信息取出预约规则，清空重复数据
        BookingRule bookingRule = hospital.getBookingRule();
        hospital.setBookingRule(null);
        //3封装数据返回
        Map<String, Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",bookingRule);
        return map;
    }

    //封装等级和地址
    private Hospital packHospital(Hospital hospital) {
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());

        return hospital;
    }
}
